<template>
  <div class="info">
    <el-row>
        <el-col :span="24" class="col-name">
            <div class="section-title">修改信息</div>
              <el-form ref="form" :model="params" label-width="80px">
                <el-form-item label="用户名">
                  <el-input v-model="params.username"></el-input>
                </el-form-item>
                <el-form-item label="密码">
                  <el-input v-model="params.password"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                  <el-radio-group v-model="params.gender">
                    <el-radio :label="0">男</el-radio>
                    <el-radio :label="1">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="段位">
                  <el-select v-model="params.grade" placeholder="请选择段位">
                    <el-option label="最强王者" :value="1"></el-option>
                    <el-option label="傲视宗师" :value="2"></el-option>
                    <el-option label="超凡大师" :value="3"></el-option>
                    <el-option label="璀璨钻石" :value="4"></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="生日">
                  <el-col :span="11">
                    <el-date-picker type="date" placeholder="选择日期" v-model="birthday" @change="checkTime"></el-date-picker>
                  </el-col>
                  <!-- <el-col style="width:60px;height:40px;" :span="2"></el-col>
                  <el-col :span="11">
                    <el-time-picker placeholder="选择时间" v-model="date2" ></el-time-picker>
                  </el-col> -->
                </el-form-item>
                <el-form-item label="头像">
                <el-upload
                    :action="upload()"
                    class="avatar-uploader"
                    :show-file-list="false"
                    :on-remove="removeImg"
                    :on-success="handleAvatarSuccess"
                    :before-upload="beforeAvatarUpload">
                    <img v-if="imageUrl" :src="imageUrl" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
                </el-form-item>
                <el-form-item label="成就达标">
                  <el-switch v-model="params.achievement"></el-switch>
                </el-form-item>
                <el-form-item label="兴趣爱好">
                  <el-checkbox-group v-model="hoba" @change="abc">
                    <el-checkbox v-for="hob in hobby" :key="hob.code" :label="hob.code">{{hob.name}}</el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
                
                <el-form-item label="个人宣言">
                  <el-input type="textarea" v-model="params.desc"></el-input>
                </el-form-item>
                
                <el-form-item>
                  <el-button type="primary" @click="onSubmit">修改</el-button>
                  <el-button @click="goback">取消</el-button>
                </el-form-item>
              </el-form>
             </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
    inject:['reload'],
     data() {
      return {
        imageUrl: '',
        birthday:new Date(),
        params:{
          username:'',
          password:'',
          gender:'',
          grade:'',
          avatar:'',
          achievement:true,
          hobby:'',
          desc:'',
        },
        time:'',
          hobby:[],
          hoba:[]
      };
    },
    methods: {
      checkTime(){
        console.log("time:",this.birthday);
        
        let year = value.getFullYear();
      let mounth = value.getMonth() + 1;
      let date = value.getDate();
      let hour = value.getHours();
      let minute = value.getMinutes();    
      if (mounth < 10) {
        mounth = "0" + mounth;
      }
      if (date < 10) {
        date = "0" + date;
      }
      if (hour < 10) {
        hour = "0" + hour;
      }
      if (minute < 10) {
        minute = "0" + minute;
      }
      this.time = `${year}-${mounth}-${date}`;
      },
      abc(value){
        console.log("bbb:",value);
        this.params.hobby = '';
        for (const code of value) {
          this.params.hobby += code;
          this.params.hobby += ",";
        }
        this.params.hobby = this.params.hobby.substring(0,this.params.hobby.lastIndexOf(","));
      },
      handleAvatarSuccess(res, file) {
        console.log("res：",res);
        if(res.code=200){
          this.imageUrl = res.data;
          this.params.avatar = res.data;
          this.$message({
                message: '上传图片成功',
                type:'success',
                duration:1*1000
          });
        }else{
          this.$message({
                message: res.message,
                type:'error',
                duration:1*1000
          });
        }
        // this.imageUrl = URL.createObjectURL(file.raw);
      },
      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;

        if (!isJPG) {
          this.$message.error('上传头像图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      },
      upload(){
          return this.$baseUrl()+"/user/upload"
      },
      removeImg(file){
        this.imageUrl = '';
      },
       onSubmit() {
        console.log('submit:',this.params);
        let formdata = {
          id:this.params.id,
	        username:this.params.username,
	        password:this.params.password,
	        avatar:this.params.avatar,
	        grade:this.params.grade,
	        gender:this.params.gender,
	        date:this.birthday,
	        achievement:this.params.achievement,
	        hobby:this.params.hobby,
          desc:this.params.desc
        }
        this.axios.post("user/change",this.$qs.stringify(formdata)).then(res=>{
          console.log("res222:",res);
          if(res.data.code==200){
            this.$message.success("修改成功");
            this.reload();
          }
        })
      },
      goback(){
        this.$router.push("userInfo");
      }
    },
    created(){
      let id = this.$route.query.id;
      console.log(id);
      this.axios.get("user/deatil/"+id).then(res=>{
            console.log("个人信息：",res);
            this.imageUrl = res.data.data.avatar;
            this.params = res.data.data;
            delete this.params['birthday'];
            var object =  this.params.hobby.split(',');
            for (const key in object) {
              if (object.hasOwnProperty(key)) {
                this.hoba.push(Number.parseInt(object[key]));
              }
            }
            console.log("aaaaa:",this.hoba);
            
        })
      this.axios.get("other/hobby/list").then(res=>{
            console.log("兴趣爱好列表：",res);
            this.hobby = res.data.data;
      })
    }
}
</script>

<style>
.section-title {
  border-bottom: solid 1px #f0f0f0;
  padding: 10px 10px;
  color: #acacac;
  background-color: #fbfbfb;
  margin: 10px 0;
}
.info{
    text-align: left;
}
/* .col-name div{
    padding: 15px;
} */
 .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 100px;
    height: 100px;
    line-height: 100px;
    text-align: center;
  }
  .avatar {
    width: 100px;
    height: 100px;
    display: block;
  }
  .el-input__inner{
    width: 300px;
  }
  .el-textarea__inner{
    width: 730px;
  }
  .el-col-11{
    width:300px;
  }
</style>